﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Face;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.Manager
{
    public partial class FrmStockManagement : Form
    {
        public FrmStockManagement()
        {
            InitializeComponent();
        }

        private void FrmStockManagement_Load(object sender, EventArgs e)
        {
            m_init();
        }

        void m_init()
        {
            this.dgMain.AutoGenerateColumns = false;
            m_bindAllStocks();
        }

        private void m_bindAllStocks()
        {
            StockManageFace face = new StockManageFace();
            List<Stock> stocks = face.FindAllStocks();
            this.dgMain.DataSource = stocks;
            lblStockNum.Text = stocks.Count.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (new FrmAddStock().ShowDialog() == DialogResult.Yes)
            {
                m_bindAllStocks();
            }        
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgMain.CurrentRow != null && this.dgMain.CurrentRow.DataBoundItem != null)
            {
                Stock stock = (Stock)this.dgMain.CurrentRow.DataBoundItem;
                frmEditStock frm = new frmEditStock(stock);
                if (frm.ShowDialog() == DialogResult.Yes)
                {
                    m_bindAllStocks();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgMain.CurrentRow != null && this.dgMain.CurrentRow.DataBoundItem != null)
            {
                if (Tool.ShowQuestionBox("确认要删除此股票吗？") == DialogResult.Yes)
                {
                    Stock stock = (Stock)this.dgMain.CurrentRow.DataBoundItem;
                    StockManageFace face = new StockManageFace();
                    try
                    {
                        face.DeleteStock(stock.Id);
                        m_bindAllStocks();
                        Tool.ShowInformationBox("删除成功!");
                    }
                    catch (Exception)
                    {
                        Tool.ShowInformationBox("删除失败!");
                    }
                }
            }
        }
    }
}
